package com.UI;

import java.util.List;

import com.Model.FacultyHoursAdapter;
import com.Model.StudentDBModel;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class FacultyHoursList extends ListActivity{
	private ListView facHoursList;
	 private List<String> facHoursRecords;
	 private String[] facHours;
	 private String facName;
	 private String courseNo;
	 private String facultyID;
	 private String[] menuItems = {"Edit Faculty Hours", "Delete Faculty Hours"};
	 protected static final int CONTEXTMENU_EDITITEM = 0;
	 protected static final int CONTEXTMENU_DELETEITEM = 1;
	 private StudentDBModel model;
	 private FacultyHoursAdapter hoursAdapter;
	 
	 @Override
	 public void onCreate(Bundle savedInstanceState){
		 super.onCreate(savedInstanceState);
		 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);		
		 setContentView(R.layout.faculty_hours_list);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.faculty_hours_title);
		 
		 Bundle extras = getIntent().getExtras();
		 courseNo = extras.getString("CourseNum");
		 
		 model = new StudentDBModel(this);
		 
		 findViewById(R.id.back).setOnClickListener(new OnClickListener() {
			 public void onClick(View v) {
				 FacultyHoursList.this.finish();

			}
		 });

		 findViewById(R.id.plus).setOnClickListener(new OnClickListener() {
			 public void onClick(View v) {
				FacultyHoursList.this.finish();
				Intent addEvent = new Intent();
				addEvent.putExtra("CourseNum", courseNo);
				addEvent.setClassName("com.UI", "com.UI.AddFacultyHours");
				addEvent.putExtra("CourseNum", courseNo);
				startActivity(addEvent);
			}
		 });
		 
		 facHoursList= getListView();
		 facHoursList.setTextFilterEnabled(true);
		 refreshFacultyHours();   
		 registerForContextMenu(facHoursList);
		 
		 facHoursList.setOnItemClickListener(new OnItemClickListener() {
		    	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
		    		facName = facHoursList.getItemAtPosition(position).toString();
		    		facultyID = courseNo + " - " + facName;
		        	Intent viewEvent = new Intent(FacultyHoursList.this, ViewFacultyHours.class);
		        	viewEvent.putExtra("FacCourseComboID", facultyID);
		        	startActivity(viewEvent);
		    		
		        	refreshFacultyHours();
		    	}
		 });
	 }
	 
	 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
			 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

			 menu.setHeaderTitle(facHours[info.position]);

			 for (int i = 0; i < menuItems.length; i++) {
				 menu.add(Menu.NONE, i, i, menuItems[i]);
			 }
	 }
	 
	 @Override
	 public boolean onContextItemSelected(MenuItem item) {
		 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		 int menuItemIndex = item.getItemId();
		 String listItemName = facHours[info.position];

		 /* Switch on the ID of the item, to get what the user selected. */
		 switch (menuItemIndex) {
		 	case CONTEXTMENU_EDITITEM:
		 		facultyID = courseNo + " - " + listItemName;
	           	Intent editHours = new Intent(FacultyHoursList.this, EditFacultyHours.class);
	           	editHours.putExtra("FacCourseComboID", facultyID);
	        	startActivity(editHours);    
	        	refreshFacultyHours();  
	        	return true; /* true means: "we handled the event". */
	        	   
		 	case CONTEXTMENU_DELETEITEM:
	        	/* Remove it from the list.*/
		 		facultyID = courseNo + " - " + listItemName;
	        	model.deleteFaculty(facultyID);
	            refreshFacultyHours();
	            return true; /* true means: "we handled the event". */
		 }
		 return false;
	 }
	 
	 public void refreshFacultyHours(){
		 
		 facHoursRecords = model.selectFacultyHours(courseNo);
		 facHours = (String[])facHoursRecords.toArray(new String[facHoursRecords.size()]);
		 
	     this.hoursAdapter = new FacultyHoursAdapter(this, R.layout.faculty_hours_row, facHoursRecords);
	     setListAdapter(this.hoursAdapter);
		 
	 }
}
